package com.cool.controller;

import com.cool.dao.UserMapper;
import com.cool.entity.User;
import com.cool.util.Encryption;
import com.cool.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "/api/users",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    public ResponseEntity<ArrayList<User>> getUsers(){
        ArrayList<User> users=userMapper.selectAll();
        if(users.size()==0) return new ResponseEntity<ArrayList<User>>(HttpStatus.NO_CONTENT);
        else return new ResponseEntity<ArrayList<User>>(users,HttpStatus.OK);
    }

    @RequestMapping(value = "/api/user",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    public ResponseEntity<User> login(@RequestParam(value = "name")String name,@RequestParam(value = "pwd")String pwd){
            User user=new User();
            user.setName(name);
            user.setPwd(pwd);
            pwd= Encryption.MD5(user.getPwd());
            user.setPwd(pwd);
        User result = userMapper.selectByNameAndPwd(user);
        if(result == null){
            return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
        }else {
            //登录成功 设置jwt
            JWTUtils util = new JWTUtils();
            //设置信息
            Map<String, Object> payload = new HashMap<String, Object>();
            payload.put("user_id", result.getId());
            try {
                String jwt = util.createJWT("jwt", "",7*24*60*60*1000, payload);
                result.setToken(jwt);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println("jwt出错了");
            }
            result.setPwd(null);
            return new ResponseEntity<User>(result,HttpStatus.OK);
        }
    }

    @RequestMapping(value = "/api/user",method = RequestMethod.POST,consumes = "application/json;charset=utf-8",produces = "application/json;charset=utf-8")
    public ResponseEntity<User> add(@RequestBody User user)
    {
        String pwd= Encryption.MD5(user.getPwd());
        user.setPwd(pwd);
        if(userMapper.selectByName(user.getName())!=null) return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
        userMapper.insert(user);
        user.setPwd(null);
        return new ResponseEntity<User>(user,HttpStatus.CREATED);
    }

    @RequestMapping(value = "/api/users/{id}",method = RequestMethod.DELETE,produces = "application/json;charset=utf-8")
    public ResponseEntity<User> del(@PathVariable int id){
            if(userMapper.selectByPrimaryKey(id)==null){
                return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
            }
            userMapper.deleteByPrimaryKey(id);
            return new ResponseEntity<User>(HttpStatus.OK);
    }

    @RequestMapping(value = "/api/user",method = RequestMethod.PUT,consumes = "application/json;charset=utf-8")
    public ResponseEntity<User> update(@RequestBody User user){
            if(userMapper.selectByPrimaryKey(user.getId())==null){
                return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
            }
            userMapper.updateByPrimaryKeySelective(user);
            int id=user.getId();
            user=userMapper.selectByPrimaryKey(id);
            return new ResponseEntity<User>(user,HttpStatus.OK);
    }

    @RequestMapping(value = "/api/users/{id}",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    public ResponseEntity<User> select(@PathVariable int id){
        User user=userMapper.selectByPrimaryKey(id);
        if(user==null){
            return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
        }
        return new ResponseEntity<User>(user,HttpStatus.OK);
    }
}
